<template>
  <view class="index">
    <view class="jtop">
      <view class="status_bar"><!-- 这里是状态栏 --></view>
      <view class="jtop_l" @tap="navigateBack"
        ><image src="../../static/img/ic_back.png"></image
      ></view>
      <!-- <view class="jtop_l"  @tap="navigateBack">取消</view> -->
      <view class="jtop_c">添加成员</view>
      <!-- <view class="jtop_r" :class="parameter?'':'no'" @click="jsave">保存</view> -->
    </view>
    <view class="tophr"></view>

    <view class="ubs">
      <view class="ub">
        <view class="ub_l">手机号</view>
        <!-- <image src="../../static/img/user/ic_right.png" class="ub_jt"></image> -->
        <input
          type="number"
          placeholder="请输入手机号"
          maxlength="11"
          :value="phone"
          @input="onKeyInput_o"
          class="ub_t"
        />
        <!-- <view >总经理</view> -->
      </view>
      <!-- <view class="ub">
        <view class="ub_l">姓名</view>
       
        <input
          type="text"
          placeholder="请输入姓名"
          :value="full_name"
          @input="onKeyInput_t"
          class="ub_t"
        />
        
      </view> -->
      <view class="ub">
        <view class="ub_l">职位</view>
        <!-- <image src="../../static/img/user/ic_right.png" class="ub_jt"></image> -->
        <input
          type="text"
          placeholder="请输入职位(选填)"
          :value="duty"
          @input="onKeyInput_c"
          class="ub_t"
        />
        <!-- <view >总经理</view> -->
      </view>
    </view>
    <!-- 	
		<view class="ubs ubs2" @click="jdelete">
			删除成员
		</view> -->

    <view class="btn-row">
      <view
        type="primary"
        class="bt primary"
        @tap="bindLogin"
        :loading="isLoading"
        >提交</view
      >
    </view>
  </view>
</template>

<script>
import request from '@/common/request.js';
import { mapState, mapMutations } from 'vuex';

export default {
  computed: {
    ...mapState(['hasLogin', 'forcedLogin']),
  },
  data() {
    return {
      repeat_click: true,
      ashowpop: false,
      corpId: '',
      phone: '',
      full_name: '',
      duty: '',
      isLoading: false,
    };
  },
  onLoad(e) {
    console.log(e);

    this.corpId = e.id;
  },
  methods: {
    onKeyInput_o: function(event) {
      this.phone = event.detail.value;
    },
    onKeyInput_t: function(event) {
      this.full_name = event.detail.value;
    },
    onKeyInput_c: function(event) {
      this.duty = event.detail.value;
    },

    // 验证
    bindLogin() {
      // if (this.phone.length < 1) {
      // 						this.repeat_click = true;
      //uni.showToast({
      // 		icon: 'none',
      // 		title: '请填写手机号'
      // 	});
      // 	return;
      // }

      if (this.phone.length < 1) {
        this.repeat_click = true;
        uni.showToast({
          icon: 'none',
          title: '请填写手机号',
        });
        return;
      }

      // if (this.duty.length < 1) {
      // 						this.repeat_click = true;
      //uni.showToast({
      // 		icon: 'none',
      // 		title: '请填写职位'
      // 	});
      // 	return;
      // }

      request({
        url: '/ucenter/corp-user',
        data: {
          corpId: this.corpId,
          phone: this.phone,
          duty: this.duty,
          // name: this.full_name,
        },

        method: 'POST',
        success: openIdRes => {
          console.log(openIdRes);

          // 已经授权了、查询到用户的数据了
          if (openIdRes.data.success == true) {
            wx.showToast({
              title: '添加成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  uni.navigateTo({
                    url: '../user/tream',
                  });
                }, 1500);
              },
            });
          } else {
            // 验证码错误
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    jsave() {
      if (!this.repeat_click) {
        return;
      }
      this.repeat_click = false;
      if (!this.parameter) {
        this.repeat_click = true;
        uni.showToast({
          title: '请输入职位',
          icon: 'none',
        });
        return;
      }

      request({
        url: '/passport/v1/corp-user',
        data: {
          id: this.id,
          fullName: this.parameter,
        },

        method: 'PUT',
        success: openIdRes => {
          console.log(openIdRes);
          if (openIdRes.data.success == true) {
            this.repeat_click = true;
            uni.showToast({
              title: '修改成功',
            });
            // uni.navigateBack()
          } else {
            this.repeat_click = true;
            uni.showToast({
              title: openIdRes.data.msg,
              icon: 'none',
            });
            this.isLoading = false;
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },

    jdelete() {
      var aa = this.user.id;
      request({
        url: '/passport/v1/corp-user/' + aa,
        method: 'DELETE',

        success: res => {
          if (res.data.success == true) {
            wx.showToast({
              title: '删除成功',
              icon: 'none',
              duration: 1500,
              success: function() {
                setTimeout(function() {
                  uni.navigateTo({
                    url: '../user/tream',
                  });
                }, 1500);
              },
            });
          } else {
            // 错误
            this.repeat_click = true;
            uni.showToast({
              title: res.data.msg,
              icon: 'none',
            });
          }
        },
        fail: () => {
          this.repeat_click = true;
          uni.showToast({
            title: '服务器开小差，请稍后~',
            icon: 'none',
          });
          return false;
        },
      });
    },
    user_edit_company() {
      uni.navigateTo({
        url: '../user/user_edit_company',
      });
    },
    user_edit_mail() {
      uni.navigateTo({
        url: '../user/user_edit_mail',
      });
    },
    user_edit_phone() {
      uni.navigateTo({
        url: '../user/user_edit_phone',
      });
    },
    user_edit_sex() {
      uni.navigateTo({
        url: '../user/user_edit_sex',
      });
    },
    user_edit_name() {
      uni.navigateTo({
        url: '../user/user_edit_name',
      });
    },
    user_head() {
      uni.navigateTo({
        url: '../user/user_head',
      });
    },

    navigateBack() {
      uni.navigateBack();
    },
  },
};
</script>

<style>
.bt {
  width: 690rpx;
  height: 94rpx;
  margin: 30rpx 0 0 30rpx;
  background: #256aff;
  border-radius: 10rpx;
  text-align: center;
  line-height: 94rpx;
  font-size: 36rpx;
  color: #ffffff;
  display: block;
  float: left;
  border: 0;
}
.bt:active {
  background: linear-gradient(
      0deg,
      rgba(0, 0, 0, 0.199437),
      rgba(0, 0, 0, 0.199437)
    ),
    #256aff;
  border-radius: 8px;
}

.ubs {
  width: 100%;
  height: auto;
  display: block;
  float: left;
  margin: 20rpx 0 0 0;
  border: 1px solid #dddddd;
  border-left: 0;
  border-right: 0;
  background: #ffffff;
}
.ubs2 {
  text-align: center;
  color: #eb5342;
  font-size: 36rpx;
  line-height: 90rpx;
}
.ub {
  width: 720rpx;
  height: 90rpx;
  display: block;
  float: right;
  line-height: 90rpx;
  border-bottom: 1px solid #dddddd;
}
.ubs .ub:last-child {
  border: 0;
}
.ub_l {
  width: 150rpx;
  height: auto;
  display: block;
  float: left;
  font-size: 34rpx;
  line-height: 90rpx;
  color: #000000;
  margin: 0rpx 0 0 0;
}
.ub_jt {
  width: 16rpx;
  height: 25rpx;
  display: block;
  float: right;
  margin: 32rpx 30rpx 0 0;
}
.ub_t {
  width: 400rpx;
  height: 40rpx;
  display: block;
  float: right;
  font-size: 32rpx;
  line-height: 40rpx;
  text-align: right;
  color: #999999;
  margin: 25rpx 20rpx 0 0;
}
.ub_t image {
  width: 36rpx;
  height: 36rpx;
  display: block;
  float: right;
  margin: 0rpx 0rpx 0 6rpx;
}
.ub_tx {
  width: 114rpx;
  height: 114rpx;
  display: block;
  float: right;
  margin: 30rpx 20rpx 30rpx 0;
  border-radius: 8rpx;
}
.uba {
  height: auto;
}
.uba .ub_l {
  margin-top: 45rpx;
}
.uba .ub_jt {
  margin-top: 75rpx;
}

.index {
  background: rgb(242, 242, 242);
  width: 100%;
  height: auto;
}
.jtop {
  width: 100%;
  height: 88rpx;
  background: #ffffff;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 1000;
  box-shadow: inset 0px -1px 0px #dddddd;
}
.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_r {
  width: auto;
  height: 88rpx;
  display: block;
  float: right;
  margin: 0rpx 20rpx 0 0;
  line-height: 88rpx;
  font-size: 32rpx;
  color: #000000;
  color: #256aff;
}
.jtop_r.no {
  opacity: 0.5;
}
.jtop_r image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
}

.jtop_lt {
  margin: 0 0 0 10rpx;
}

.jtop_l {
  width: auto;
  height: 88rpx;
  display: block;
  float: left;
  font-size: 32rpx;
  color: #000000;
  line-height: 88rpx;
  margin: 0 0 0 30rpx;
}
.jtop_l image {
  width: 48rpx;
  height: 48rpx;
  display: block;
  float: left;
  margin: 20rpx 0 20rpx 0;
}
.jtop_lt {
  margin: 0 0 0 10rpx;
}

/* .jtop_r{width: 48rpx; height: 48rpx; display: block;float: right; margin: 20rpx 20rpx 0 0;} */
/* .jtop_rl{margin: 20rpx 30rpx 0 0;} */
/* .jtop_r image{width: 48rpx; height: 48rpx; display: block;float: left;} */
.jtop_c {
  width: 70%;
  height: 48rpx;
  line-height: 48rpx;
  display: block;
  float: left;
  position: fixed;
  top: 20rpx;
  left: 15%;
  text-align: center;
  font-size: 36rpx;
  color: #000000;
}
.jtop_c image {
  width: 36rpx;
  height: 36rpx;
  display: inline-block;
  float: none;
  margin-bottom: -5rpx;
  margin-left: 5rpx;
}
.jtop_b {
  width: 100%;
  height: auto;
  background: #ffffff;
  position: fixed;
  top: 88rpx;
  left: 0;
  z-index: 1001;
}
.tophr {
  width: 100%;
  height: 88rpx;
}

input {
  width: auto;
  height: 50rpx;
  display: block;
  float: right;
  margin: 20rpx 30rpx 0 0;
  text-align: right;
  line-height: 50rpx;
  font-size: 34rpx;
  color: #999999;
}
</style>
